<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ page import = "java.util.*" %>
<%@ page import = "com.sinodata.bsm.common.vo.*" %>
<%@ include file="../include/taglib.jsp"%>
<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragrma","no-cache");
response.setDateHeader("Expires",0);
List<Attribute> attributes = (List<Attribute> )request.getAttribute("attributes");
Map<Long,String> map =(Map<Long,String>) request.getAttribute("attVals");
ResView lastView = (ResView)session.getAttribute("lastView");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>查看资源</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>  
    <%@ include file="../include/common.jsp"%>
    <%@ include file="../include/form.jsp"%>
    <%@ include file="../include/table.jsp"%>
    <%@ include file="../include/tree.jsp"%>
   	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/default/bsm/sub_menu.css" />
	<script type=text/javascript>
		function refreshViewTree()
		{	
			var tree = window.parent.getResTree();
			var nodes = tree.getSelectedNodes();
			if (nodes.length>0) {
				nodes[0].name = $("#name").val();
				tree.updateNode(nodes[0]);
			}
		}
		ref =function (){
			var obTable = $("#childRes").dataTable();
			obTable.fnClearTable();
			 var tree = window.parent.getResTree();
			 var selectedNode = tree.getSelectedNodes()[0];
			 tree.reAsyncChildNodes(selectedNode, "refresh");
		}
		function showRelTree() {
			var cityObj = $("#resRelName");
			var cityOffset = $("#resRelName").offset();
			var rTree = $.fn.zTree.getZTreeObj("relRelTree");
			if(rTree!=null){
				var nodes = rTree.getSelectedNodes();
				if(nodes.length==0){
					var defaultNode = rTree.getNodeByParam("id", '${relationType.id}', null);
					rTree.selectNode(defaultNode);
				}
				$("#relContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
				$("body").bind("mousedown", onRelTreeDown);
			}
		}
		function resetRelTree()
		{	var rTree = $.fn.zTree.getZTreeObj("relRelTree");
			var defaultNode = rTree.getNodeByParam("id", '${relationType.id}', null);
			rTree.selectNode(defaultNode);
			$("#resRelId").attr("value", defaultNode.id);
			$("#resRelName").attr("value",defaultNode.name);
		}
		function onRelTreeDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "resRelId" || event.target.id == "relContent" || $(event.target).parents("#relContent").length>0)) {
				$("#relContent").fadeOut("fast");
				$("body").unbind("mousedown", onRelTreeDown);
			}
		}
		function relNodeClick(e, treeId, treeNode) {
			$("#relContent").fadeOut("fast");
			$("body").unbind("mousedown", onRelTreeDown);
			$("#resRelId").attr("value", treeNode.id);
			$("#resRelName").attr("value",treeNode.name);
			$("#resRelName").blur();
		}
		function modifyIt(){
			$.formValidator.initConfig({ 
				formid:"resForm",
				onerror:function(msg){},
				onsuccess:function(){
						$.ajax({
							type:'post',
							url:'res!update.action',
							data:$('#resForm').serialize(),
							global:false,
							async:false,
							cache:false,
							dataType:'json',
							success:function(data,textStatus){
								getTopWin(window).getMessageBox().tip('保存资源成功。', 'success',{timeout: 1500});
								refreshViewTree(); 
									$("#name").attr("readonly",true).unFormValidator(true);
									$("#desc").attr("readonly",true);
									$("#modifyBt").show();
									$("#btgroup").hide();
							},
							error:function(){
								$.jBox.alert('服务器内部错误,请联系管理员','错误');
							}
	   					   });
				     return false;
				}});
			var resRelTreeConfig = {
				async: {
					enable: true,
					url:"res-relation!typeTree.action",
					dataType: "text",
					autoParam:["id"]
				},
				callback: {
					onClick: relNodeClick
				},
				view: {
					dblClickExpand: false,
					showLine: false
				}
			};
			$("#name").formValidator({onshow:"输入资源名称",onfocus:"请输入1-20个字符的资源名称"})
			              .inputValidator({min:1,max:20,onerror:"资源名称长度不正确"});
			$("#desc").attr("readonly",false);
			$("#name").attr("readonly",false);
			$("#modifyBt").hide();
			$("#btgroup").show();
			$.fn.zTree.init($("#relRelTree"),resRelTreeConfig);
		}
		function resetIt()
		{	
			document.forms[0].reset();
			$.formValidator.resetTipState("1")
			resetRelTree();
		}
		function back()
		{
			document.forms[0].reset();
			resetRelTree();
			$("#name").attr("readonly",true).unFormValidator(true);
			$("#desc").attr("readonly",true);
			$("#modifyBt").show();
			$("#btgroup").hide();
		}
		function deleteRes(deleteid,isSelf){
		  var submit = function(v, h, f){
		  	if (v == 'ok'){
		  		getTopWin(window).getMessageBox().tip("删除资源中...", 'loading');
		  			var viewId ='<%=lastView.getId()%>';
					var myparam={};
					if(isSelf==1){
						myparam = {"id":deleteid,"viewId":viewId}
					}else{
						myparam = {"id":deleteid,"pId":$("#id").val()}
					}
		  		$.ajax({
					type:'post',
					url:'res!delete.action',
					data:myparam,
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						if(isSelf==1){
							getTopWin(window).getMessageBox().tip('删除资源成功。', 'success'); 
							window.parent.location.reload();
						}
						else{
							window.setTimeout(function () {
								getTopWin(window).getMessageBox().tip('删除资源成功。', 'success'); 
								var obTable = $("#childRes").dataTable();
								obTable.fnClearTable();
								 
								 var tree = window.parent.getResTree();
				 				 var selectedNode = tree.getSelectedNodes()[0];
				 				 tree.reAsyncChildNodes(selectedNode, "refresh");
			 				  
							},1500);
						}
						
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
					}
				});
		  	}
		  }
		  getTopWin(window).getMessageBox().confirm('确定删除?','提示',submit);
		}
		
		function openResRelationWindow(){
			var url ='iframe:resource/res-relation!input.action?parentId=${res.id}';
			var opt =  {title:'添加父子关系',id:'resRelationWindow',dragLimit:false,width:550,height:230,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
			getTopWin(window).openjBox(url,opt,'resRelationWindow',window);
		}
		
		$(document).ready(function(){
			$("#childRes").dataTable({
					"bProcessing": true, 
					"sAjaxSource":'res!list.action?id=${res.id}',
					"bServerSide": true,
					"sServerMethod": 'POST',
					"bPaginate":false,
					"bSort": true,
					"bFilter":false,
					"sPaginationType": 'full_numbers',
					"aoColumns":[
					{"mDataProp":"operate","bSortable": false,"fnCreatedCell":function(nTd, sData, oData, iRow, iCol){
						$(nTd).html("<div style='width:25px;'><a href='#' onclick='deleteRes("+oData.id+");'><img src='../style/default/bsm/images/delete.gif' title='删除' /></a></div>");
					}},
					{"mDataProp":"name","bSortable": false},
					{"mDataProp":"resTypeName","bSortable": false},
					{"mDataProp":"ip" },
					{"mDataProp":"probeName","bSortable": false},
					{"mDataProp":"desc","bSortable": false},
					{"mDataProp":"resTypeId","bVisible":false},
					{"mDataProp":"probeId","bVisible":false}
				], 
					 "oLanguage": { 
					 				"sZeroRecords": "没有找到记录",
									"sInfo":"",
									"sInfoEmpty": ""
					}
			});
	  });
	</script>
	<style type="text/css">
		.ztree {margin-top: 5px;border: 1px solid #617775;background: #f0f6e4;width:220px;height:350px;overflow-y:scroll;overflow-x:auto;}
	</style>
  </head>
  <body>
  <h2 class="title-h2" onclick="accordionDiv(this)" ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>本资源信息</h2>
  <div id="resDiv">
	<form   name="resForm" id="resForm" method="post">
	<input type="hidden" name="id" id="id" value="${res.id }" />
	<input type="hidden" name="pId" id="pId" value="${parentRes.id}" />
    <table class="table_eidt" id="restable" cellpadding="0" cellspacing="0">
		<tbody>
			<tr>
	    		<td class="td_left" style="width:150px">父资源</td>
	    		<td class="td_right" style="width:340px;">${parentRes.name}</td>
	    		<td class="td_left" style="width:150px">资源类型</td>
	    		<td class="td_right">
	    			<a href="res-type!model.action?id=${resType.id}" target="blank" title="点击链接可查看资源类型明细" >${resType.name}</a>
	    		</td>
	    	</tr>
	    	<tr>
	    		<td class="td_left">资源IP</td>
	    		<td class="td_right">${res.ip}</td>
				<td class="td_left">监控代理</td>
	    		<td class="td_right">${probe.name}
	    		 <c:choose >
	    		 	<c:when test="${probe.status==3}">
	    		 		<img title="已启动" id='probeStatu' src='${pageContext.request.contextPath}/style/default/bsm/images/proberun.png'  />
	    		 	</c:when>
	    		 	<c:when test="${probe.status!=3}">
	    		 		<img title='未启动' id='probeStatu' src='${pageContext.request.contextPath}/style/default/bsm/images/probestop.png'  />
	    		 	</c:when>
	    		 </c:choose>
	    		</td>
	    	</tr>

	    	<tr>
	    		<td class="td_left">资源名称</td>
	    		<td class="td_right">${res.name }</td>
				<td class="td_left">与父资源关系</td>
	    		<td class="td_right">${relationType.name}</td>
	    	</tr>
	    	<tr id="codeTr" >
	    		
	    	<%
	    		for(int i=0;i<attributes.size();i++){
		    		Attribute att = attributes.get(i);	
	    	%>
	    		<td class="td_left">
					 <%=att.getName() %>
				</td>
	    		<td class="td_right" >
	    		<%
	    		
	    			out.print(map.get(att.getId())==null ? "&nbsp;":map.get(att.getId()) );
	    		%>
				</td>
	    	<% 		
	    			if((i+1)%2==0){
	    				out.println("</tr>");
	    			}
	    			
	    			if(i==(attributes.size()-1)&&attributes.size()%2==0){
	    				out.println("<tr>");
	    			}
	    		}
	    	%>
	    		<td class="td_left">
					 描述
				</td>
	    		<td class="td_right"  >${res.desc }</td>
			</tr>

		</tbody>
    </table>
    			
    	<div   class="btn-div">			
   			<input type="button" id ="modifyBt" value="修改" onclick="location.assign('res!initUpdate.action?id=${res.id}&pId=${parentRes.id}')" class="btn-4"/>
   			<c:if test="${parentRes==null}">
   				<input type="button" id ="modifyBt" value="删除本资源" onclick="deleteRes('${res.id}',1);" class="btn-4" style="padding-left:5px;"/>
   			</c:if>
   			<span id="btgroup" style="display:none;">
   				<button type="submit" id ="saveBt" value="保存" class="btn-4">保存</button>
   				<button type="button" onclick='resetIt();' class="btn-4">重置</button>
   				<button type="button" onclick='back();' class="btn-4">取消</button>
   			</span>
    	</div>	
   </form>
</div>
	<h2 class="title-h2" onclick="accordionDiv(this)" ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>子资源列表</h2> 
	<div id="childResDiv">
		<div class ="toolbar" id="toolbar">
			<a href="#" class="page_add" onclick="openResRelationWindow()" >新增父子关系</a>
	 	</div>
		<table width="100%"  id="childRes" cellpadding="0" cellspacing="0" border="0" class="display"  >
				<thead>
	    	 		<tr>
	    	 			<th width="25px">操作</th>
	    	 			<th width="180px">子资源名称</th>
	    	 			<th width="180px">子资源类别</th>
	    	 			<th width="120px">子资源IP</th>
	    	 			<th width="120px">所在probe</th>
	    	 			<th>描述</th>
	    	 		</tr>
	    	 	</thead>
	    </table>
   </div>
     <div id="relContent" class="menuContent" style="display:none; position: absolute;">
	  <ul id="relRelTree" class="ztree" style="margin-top:0;padding-left:0;  width:220px; height:120px; "></ul>
   </div>
  </body>
</html>
